/**
 * 
 */
package edu.fjnu.fundsys.dao.impl;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import edu.fjnu.fundsys.dao.FundTransinfoDao;
import edu.fjnu.fundsys.domain.Account;
import edu.fjnu.fundsys.domain.FundTransinfo;
import edu.fjnu.fundsys.utils.DBUtils;

/**
 * @author BaiXiang
 *
 */
public class FundTransinfoDaoJDBCImpl implements FundTransinfoDao {

	private static final String ADD_FUND_TRANSINFO = "insert into fund_transinfo values(seq_fund_trans_id.nextval,?,?,?,?,?,to_date('2012-12-31 15:45:10','YYYY-MM-DD HH24:MI:SS'))";
	private static final String LOAD_ALL = "select * from fund_transinfo order by trans_id desc";
	private static final String LOAD_BY_ACCOUNTNO = "select * from fund_transinfo where account_no = ? order by trans_id desc";
	private static final String CNT_INFOS = "select count(*) cnt_infos from fund_transinfo where account_no = ? order by trans_id desc";
	
	public void add(FundTransinfo fundtransinfo) {
		// TODO Auto-generated method stub
		Connection conn = DBUtils.getInstance().getConn();
		PreparedStatement pstmt = null;
		
		try {
			pstmt = conn.prepareStatement(ADD_FUND_TRANSINFO);
			pstmt.setString(1, fundtransinfo.getTransType());
			pstmt.setInt(2, fundtransinfo.getAccountNo());
			pstmt.setInt(3, fundtransinfo.getFundNo());
			pstmt.setInt(4, fundtransinfo.getAmount());
			pstmt.setDouble(5, fundtransinfo.getPrice());
			
			pstmt.executeUpdate();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally{
			DBUtils.getInstance().ReleaseRes(conn, pstmt, null);
		}
	}

	public List<FundTransinfo> loadAll() {
		// TODO Auto-generated method stub
		Connection conn = DBUtils.getInstance().getConn();
		PreparedStatement pstmt = null;
		ResultSet rset = null;
		List<FundTransinfo> fundTransinfoList = null;
		
		try{
			pstmt = conn.prepareStatement(LOAD_ALL);
			rset = pstmt.executeQuery();
			fundTransinfoList  = new ArrayList<FundTransinfo>();
			while(rset.next()){
				FundTransinfo fundTransinfo = new FundTransinfo();
				
				fundTransinfo.setTransId(rset.getInt("trans_id"));
				fundTransinfo.setTransType(rset.getString("trans_type"));
				fundTransinfo.setAccountNo(rset.getInt("account_no"));
				fundTransinfo.setFundNo(rset.getInt("fund_no"));
				fundTransinfo.setAmount(rset.getInt("amount"));
				fundTransinfo.setPrice(rset.getDouble("price"));
				fundTransinfo.setTransTime(rset.getString("trans_time"));
				
				fundTransinfoList.add(fundTransinfo);
			}
		}catch(SQLException e){
			e.printStackTrace();
		}finally{
			DBUtils.getInstance().ReleaseRes(conn, pstmt, rset);
		}
		return fundTransinfoList;
	}

	public List<FundTransinfo> loadByAccountNo(Integer accountno) {
		// TODO Auto-generated method stub
		Connection conn = DBUtils.getInstance().getConn();
		PreparedStatement pstmt = null;
		ResultSet rset = null;
		List<FundTransinfo> fundTransinfoList = null;
		
		try{
			pstmt = conn.prepareStatement(LOAD_BY_ACCOUNTNO);
			pstmt.setInt(1, accountno);
			rset = pstmt.executeQuery();
			fundTransinfoList  = new ArrayList<FundTransinfo>();
			while(rset.next()){
				FundTransinfo fundTransinfo = new FundTransinfo();
				
				fundTransinfo.setTransId(rset.getInt("trans_id"));
				fundTransinfo.setTransType(rset.getString("trans_type"));
				fundTransinfo.setAccountNo(rset.getInt("account_no"));
				fundTransinfo.setFundNo(rset.getInt("fund_no"));
				fundTransinfo.setAmount(rset.getInt("amount"));
				fundTransinfo.setPrice(rset.getDouble("price"));
				fundTransinfo.setTransTime(rset.getString("trans_time"));
				
				fundTransinfoList.add(fundTransinfo);
			}
		}catch(SQLException e){
			e.printStackTrace();
		}finally{
			DBUtils.getInstance().ReleaseRes(conn, pstmt, rset);
		}
		return fundTransinfoList;
	}

	public int cntInfos(Integer accountNo) {
		// TODO Auto-generated method stub
		Connection conn = DBUtils.getInstance().getConn();
		PreparedStatement pstmt = null;
		ResultSet rset = null;
		int cnt = 0;
		
		try{
			pstmt = conn.prepareStatement(CNT_INFOS);
			pstmt.setInt(1, accountNo);
			rset = pstmt.executeQuery();

			if(rset.next()){
				cnt = rset.getInt("cnt_infos");
			}
		}catch(SQLException e){
			e.printStackTrace();
		}finally{
			DBUtils.getInstance().ReleaseRes(conn, pstmt, rset);
		}
		return cnt;
	}

	public List<FundTransinfo> getSelectInfos(Integer accountNo, Integer begin,
			Integer end) {
		// TODO Auto-generated method stub
		String sql = "select * from (select rownum rn, a.* from (select * from fund_transinfo where account_no ="+accountNo+"order by trans_id desc) a where rownum<=? ) where rn>=?";
		Connection conn = DBUtils.getInstance().getConn();
		PreparedStatement pstmt = null;
		ResultSet rset = null;
		List<FundTransinfo> fundTransinfoList = null;
		
		try{
			pstmt = conn.prepareStatement(sql);
			pstmt.setInt(1, end);
			pstmt.setInt(2, begin);
			rset = pstmt.executeQuery();
			fundTransinfoList  = new ArrayList<FundTransinfo>();
			while(rset.next()){
				FundTransinfo fundTransinfo = new FundTransinfo();
				
				fundTransinfo.setTransId(rset.getInt("trans_id"));
				fundTransinfo.setTransType(rset.getString("trans_type"));
				fundTransinfo.setAccountNo(rset.getInt("account_no"));
				fundTransinfo.setFundNo(rset.getInt("fund_no"));
				fundTransinfo.setAmount(rset.getInt("amount"));
				fundTransinfo.setPrice(rset.getDouble("price"));
				fundTransinfo.setTransTime(rset.getString("trans_time"));
				
				fundTransinfoList.add(fundTransinfo);
			}
		}catch(SQLException e){
			e.printStackTrace();
		}finally{
			DBUtils.getInstance().ReleaseRes(conn, pstmt, rset);
		}
		return fundTransinfoList;
	}
	
	

}
